/*
 * @Author: Guiter 1336263157@qq.com
 * @Date: 2023-11-29 20:00:56
 * @LastEditors: Guiter 1336263157@qq.com
 * @LastEditTime: 2023-11-29 20:22:11
 * @FilePath: \nuxt3-music-player\src\stores\music.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import type { MusicUrl } from '~/types/music';

/* 用户的仓库 */
const useMusicStore = defineStore('music', {
  persist: true,
  state: () => {
    return {
      audio: new Audio(),
      audioDuration: 0 /* 音乐对象时间总长 */,
      audioCurrent: 0 /* 音频当前播放时间 */,
      // audioVolume: Number(localStorage.getItem(key.volume)) || 60,/* 音频声音 */
      audioVolume: 40,
      audioPlay: false /* 音频播放状态 */,
      audioName: '暂无音乐' /* 音乐名字 */,
      audioLove: 0,
      playProgress: 0 /* 音频播放进度 */,
      url: '' /* 音乐路径 */,
      id: -1 /* 音乐序号 */,
      img: 'https://img.aidotu.com/down/jpg/20200827/52158ceea6f2630d08867b0899aa6bc9_18497_240_240.jpg' /* 音乐相册 */,
      index: -1 /* 记录音乐播放第几个 */
    };
  },
  actions: {
    /**
     * 开始播放音乐
     * @param lists
     * @param index
     */
    playMusic(lists: MusicUrl, index: number) {
      // console.log(lists)
      // console.log(this.audio.currentTime)
      // console.log(lists)
      console.log(lists);
      console.log(this.audio);
      this.audio.src = lists.url;
      this.audio.volume = this.audioVolume / 100;
      this.audio
        .play()
        .then(() => {
          console.log('播放成功');
          // this.id = lists.id
          // this.img = this.musicInfo.al.picUrl
          // this.url = lists.url
          // this.audioPlay = true
          // this.audioName = this.musicInfo.name
          // this.audioDuration = this.musicInfo.dt
          // this.audioCurrent = this.audio.currentTime
          // this.index = index
          return true;
        })
        .catch((error) => {
          throw error;
        });
    }
  }
});

export default useMusicStore;
